<?php

namespace app\order\logic;
use app\order\logic\BasicLogic;
use app\order\model\Order as OrderModel;
use think\facade\Db;

class OrderTrackLogic extends BasicLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new OrderModel();
    }

    /**
     * @desc  物流轨迹列表接口
     * @date  2024/4/17
     * @author luckyXiong
     */
    public function getOrderTrackList($params = [])
    {
        $shop_id = $params['shop_id'] ?? [];
        $limit = $params['limit'] ?? 10;
        $createtime = $params['createtime'] ?? '';
        $tracking_number = $params['tracking_number'] ?? '';
        $orderId = $params['order_id'] ?? '';
        $where = [];
        
        if (!empty($shop_id) && $shop_id > 0) {
            $where[] = ['b.shop_id', '=', (int)$shop_id];
        }

        if (!empty($tracking_number)) {
            $where[] = ['b.tracking_number', 'like', '%' . $tracking_number . '%'];
        }
        
        if (!empty($createtime)) {
            $createtimeArr = explode(',', $createtime);
            $where[] = ['b.createtime', '>=', $createtimeArr[0]];
            $where[] = ['b.createtime', '<=', $createtimeArr[1]];
        }

        if ($orderId) {
            $order_ids = trim($orderId);
            $order_ids = array_filter(array_map('trim', explode("\n", $order_ids)));
            if (!empty($order_ids)) {
                $where[] = ['b.order_id', 'in', $order_ids];
            }
        }

        $list = Db::name('order_track')
            ->field('a.sub_status,b.id,b.shop_id,b.order_id,b.status,b.createtime,b.tracking_number,c.name as shop_name')
            ->alias('a')
            ->join('ba_order b','b.tracking_number = a.number', 'left')
            ->join('ba_shops c', 'b.shop_id = c.id', 'left')
            ->where($where)
            ->order('a.time_iso desc')
            ->group('a.number')
            ->paginate($limit);
        $lists = $list->items();
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }


















}